package com.duanjw.bank.rate.handler;

import cn.hutool.json.JSONObject;
import com.duanjw.bank.rate.core.BankRate;
import com.duanjw.bank.rate.core.BankRateHandler;
import com.duanjw.bank.rate.core.DateFormatUtil;
import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * 网商银行
 *
 * @author duanjw
 * @date 2024/4/1
 */
public class WangShangBanRateHandler implements BankRateHandler {
    @Override
    public BankRate getBankRate() throws IOException {
        Map<String, String> headers = new HashMap<>();
        headers.put("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36"); // 仿真
        Connection connect = Jsoup.connect("https://render.mybank.cn/p/c/18mvp2sszxhc/am2.html");
        Document responseDoc = connect.headers(headers).ignoreContentType(true).get();

        Element script = responseDoc.select("script").get(7);
        Document scriptToDocument = get(script.data());
        Element table = scriptToDocument.select("table").get(1);

        String kaiShiZhiXingRiQi = scriptToDocument.select("div").text();
        String huoQiCunKuanLiLv = table.selectXpath("tbody/tr[3]/td[2]").text();

        String zhengCunZhengQu3YueLiLv = table.selectXpath("tbody/tr[3]/td[4]").text();
        String zhengCunZhengQu6YueLiLv = table.selectXpath("tbody/tr[3]/td[5]").text();
        String zhengCunZhengQu12YueLiLv = table.selectXpath("tbody/tr[3]/td[6]").text();
        String zhengCunZhengQu24YueLiLv = table.selectXpath("tbody/tr[3]/td[7]").text();
        String zhengCunZhengQu36YueLiLv = table.selectXpath("tbody/tr[3]/td[8]").text();
        String zhengCunZhengQu60YueLiLv = null;

        String lingCunZhengQu12YueLiLv = null;
        String lingCunZhengQu36YueLiLv = null;
        String lingCunZhengQu60YueLiLv = null;

        String dingHuoLiangBian3YueLiLv = null;
        String dingHuoLiangBian6YueLiLv = null;
        String dingHuoLiangBian12YueLiLv = null;

        String tongZhiCunKuan1TianLiLv = null;
        String tongZhiCunKuan7TianLiLv = table.selectXpath("tbody/tr[3]/td[3]").text();

        String xieDingCunKuanLiLv = null;

        return new BankRate(DateFormatUtil.format3(kaiShiZhiXingRiQi), huoQiCunKuanLiLv, zhengCunZhengQu3YueLiLv, zhengCunZhengQu6YueLiLv, zhengCunZhengQu12YueLiLv, zhengCunZhengQu24YueLiLv, zhengCunZhengQu36YueLiLv, zhengCunZhengQu60YueLiLv, lingCunZhengQu12YueLiLv, lingCunZhengQu36YueLiLv, lingCunZhengQu60YueLiLv, dingHuoLiangBian3YueLiLv, dingHuoLiangBian6YueLiLv, dingHuoLiangBian12YueLiLv, tongZhiCunKuan1TianLiLv, tongZhiCunKuan7TianLiLv, xieDingCunKuanLiLv);
    }

    @Override
    public String getBankName() {
        return "网商银行";
    }

    private Document get(String input) {

        int startIndex = input.indexOf('{');  // 找到第一个左括号的位置
        int endIndex = input.lastIndexOf('}'); // 找到最后一个右括号的位置
        String jsonString = input.substring(startIndex, endIndex + 1); // 提取括号内的内容

        String content = new JSONObject(jsonString).getStr("content");
        return Jsoup.parse(content);
    }
}
